package org.keyyond.ms.commonservice.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.keyyond.ms.commonservice.model.FDFBBimsSysMessage;
import org.keyyond.ms.commonservice.service.MsCommonMessageLogService;
import org.keyyond.ms.commonservice.service.MsSysMessageService;
import org.keyyond.ms.commonservice.util.CommonBeanUtils;
import org.keyyond.ms.core.model.MsCommonMessageLogVo;
import org.keyyond.ms.core.util.CommonBeanUtil;
import org.keyyond.ms.core.util.Constant;
import org.keyyond.ms.core.util.ConstantResult;
import org.keyyond.ms.core.util.JsonUtil;
import org.keyyond.ms.core.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@RestController
@RequestMapping("/sdbd/common")
@RefreshScope
public class SDBDCommonController {

	private static Logger logger = Logger.getLogger(SDBDCommonController.class);

	@Autowired
	private MsCommonMessageLogService messageLogService;

	@Autowired
	private CommonBeanUtils beanUtils;

	@Autowired
	private MsSysMessageService sysMessageService;

	@RequestMapping(value = "/sendMessageInSite", method = RequestMethod.POST)
	public String sendMessageInSite(HttpServletRequest req) {
		MsCommonMessageLogVo logVo = new MsCommonMessageLogVo();
		String traceId = req.getHeader(Constant.CONST_REQUEST_HEADER_TRACEID);
		logVo.setTraceid(traceId);
		logVo.setLogtype(org.keyyond.ms.commonservice.util.Constant.CONST_LOG_TYPE_COMMON_SERVICE);
		logVo.setSendtype(org.keyyond.ms.commonservice.util.Constant.CONST_SEND_TYPE_COMMON_SERVICE_SUCCESS);
		logVo.setContent(JsonUtil.toJson(req.getParameterMap()));

		// TODO:加入其它参数
		String sheettype = req.getParameter("sheettype");
		String username = req.getParameter("username");
		String messageContent = req.getParameter("messageContent");
		String template = req.getParameter("template");
		String refSheetId = req.getParameter("refSheetId");
		String messageType = req.getParameter("messageType");
		String pageUrl = req.getParameter("pageUrl");

		// 参数校验
		if (StringUtils.isEmpty(sheettype) || StringUtils.isEmpty(username) || StringUtils.isEmpty(messageContent)
				|| StringUtils.isEmpty(messageType) || StringUtils.isEmpty(refSheetId)) {
			logVo.setReturnmessage(Constant.CONST_PARAM_ERROR_RESULT);
			beanUtils.addMessageLog(logVo);
			return Constant.CONST_PARAM_ERROR_RESULT;
		} else {
			FDFBBimsSysMessage bimsSysMessageVo = new FDFBBimsSysMessage();
			bimsSysMessageVo.setUserid(username);
			bimsSysMessageVo.setCreateby(Constant.BY_ENGINE);
			bimsSysMessageVo.setMessagecontent(messageContent);
			bimsSysMessageVo.setMessagetype(messageType);
			bimsSysMessageVo.setModifyby(Constant.BY_ENGINE);
			bimsSysMessageVo.setPageurl(StringUtil.isNullString(pageUrl));
			bimsSysMessageVo.setRefsheetid(refSheetId);
			bimsSysMessageVo.setSheettype(sheettype);
			bimsSysMessageVo.setStatus(Constant.SYS_MESSAGE_STATE_NOT_READ);
			try {
				String seqno = sysMessageService.addMessageInSite(bimsSysMessageVo);
				JSONArray dataArr = new JSONArray();
				dataArr.add(seqno);
				logVo.setReturnmessage(dataArr.toString());
				beanUtils.addMessageLog(logVo);
				return CommonBeanUtil.returnResult(true, dataArr);
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
				return Constant.FALL_BACK_INFO;
			}
		}
	}

	@RequestMapping(value = "/getUnreadInfo", method = RequestMethod.GET)
	public String getUnreadInfo(HttpServletRequest req) {
		MsCommonMessageLogVo logVo = new MsCommonMessageLogVo();
		String traceId = req.getHeader(Constant.CONST_REQUEST_HEADER_TRACEID);
		logVo.setTraceid(traceId);
		logVo.setLogtype(org.keyyond.ms.commonservice.util.Constant.CONST_LOG_TYPE_COMMON_SERVICE);
		logVo.setSendtype(org.keyyond.ms.commonservice.util.Constant.CONST_SEND_TYPE_COMMON_SERVICE_SUCCESS);
		logVo.setContent(JsonUtil.toJson(req.getParameterMap()));

		try {
			String username = req.getParameter("username");
			String messageType = req.getParameter("messageType");
			String status = req.getParameter("status");
			List<FDFBBimsSysMessage> list = sysMessageService.selectMessagesByUseridAndStatus(username, status,
					messageType);
			JSONArray dataArr = new JSONArray();
			dataArr = JSONArray.fromObject(list);
			logVo.setReturnmessage(dataArr.toString());
			beanUtils.addMessageLog(logVo);
			return CommonBeanUtil.returnResult(true, dataArr);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return Constant.FALL_BACK_INFO;
		}
	}

	@RequestMapping(value = "/updateInfoStatus", method = RequestMethod.POST)
	public String updateInfoStatusToReaded(HttpServletRequest req) {

		try {
			MsCommonMessageLogVo logVo = new MsCommonMessageLogVo();
			String traceId = req.getHeader(Constant.CONST_REQUEST_HEADER_TRACEID);
			logVo.setTraceid(traceId);
			logVo.setLogtype(org.keyyond.ms.commonservice.util.Constant.CONST_LOG_TYPE_COMMON_SERVICE);
			logVo.setSendtype(org.keyyond.ms.commonservice.util.Constant.CONST_SEND_TYPE_COMMON_SERVICE_SUCCESS);
			logVo.setContent(JsonUtil.toJson(req.getParameterMap()));
			// TODO:加入其它参数
			String seqno = req.getParameter("seqno");
			String status = req.getParameter("status");

			// 参数校验
			if (StringUtils.isEmpty(seqno) || StringUtils.isEmpty(status)) {
				logVo.setReturnmessage(Constant.CONST_PARAM_ERROR_RESULT);
				beanUtils.addMessageLog(logVo);
				return Constant.CONST_PARAM_ERROR_RESULT;
			} else {
				FDFBBimsSysMessage oldRecord = sysMessageService.selectByPrimaryKey(seqno);
				if (oldRecord == null) {
					return Constant.CONST_PARAM_ERROR_RESULT;
				} else {
					oldRecord.setSeqno(seqno);
					oldRecord.setStatus(status);
					int resultCount = sysMessageService.updateRecordsBySeqno(oldRecord);
					if(resultCount < 1)
					{
						return Constant.FALL_BACK_INFO;
					}
					else
					{
						JSONArray dataArray = new JSONArray();
						dataArray.add(seqno);
						logVo.setReturnmessage(dataArray.toString());
						beanUtils.addMessageLog(logVo);
						return CommonBeanUtil.returnResult(true, dataArray);
					}
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return Constant.FALL_BACK_INFO;
		}
	}

	@RequestMapping(value = "/updateMessageToReaded", method = RequestMethod.POST)
	public String updateMessageToReaded(HttpServletRequest req) {

		try {
			MsCommonMessageLogVo logVo = new MsCommonMessageLogVo();
			String traceId = req.getHeader(Constant.CONST_REQUEST_HEADER_TRACEID);
			logVo.setTraceid(traceId);
			logVo.setLogtype(org.keyyond.ms.commonservice.util.Constant.CONST_LOG_TYPE_COMMON_SERVICE);
			logVo.setSendtype(org.keyyond.ms.commonservice.util.Constant.CONST_SEND_TYPE_COMMON_SERVICE_SUCCESS);
			logVo.setContent(JsonUtil.toJson(req.getParameterMap()));

			// TODO:加入其它参数
			String username = req.getParameter("username");
			String messageType = req.getParameter("messageType");

			// 参数校验
			if (StringUtils.isEmpty(username) || StringUtils.isEmpty(messageType)) {
				logVo.setReturnmessage(Constant.CONST_PARAM_ERROR_RESULT);
				beanUtils.addMessageLog(logVo);
				return Constant.CONST_PARAM_ERROR_RESULT;
			} else {
				int resultCount = sysMessageService.updateRecordsStatusToReaded(username, messageType);
				JSONArray dataArr = new JSONArray();
				dataArr.add(resultCount);
				logVo.setReturnmessage(dataArr.toString());
				beanUtils.addMessageLog(logVo);
				return CommonBeanUtil.returnResult(true, dataArr);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return Constant.FALL_BACK_INFO;
		}
	}

	@RequestMapping(value = "/testApi", method = RequestMethod.POST)
	public String testApi(HttpServletRequest req) {
		return "Hello World";
	}
}
